import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'
import _ from 'lodash';
import routes from './routes'
import {setPageTitle} from '../common/utils.js'
import { $nprogress, $utils, $menuUtils } from '@jzl/utils'
import {useCommonStore, useMultiTagsStore} from "@jzl/stores";

import { Page404} from "@jzl/comps";
import {defineAsyncComponent, resolveComponent} from "vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_BASE_URL),
  linkActiveClass: 'active',
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        title: "登录"
      },
      // component: () => import('../views/login.vue')
      component: defineAsyncComponent(() =>
        Promise.resolve({ template: '<Login />' })
      )
    },
    {
      path: '/',
      name: 'index',
      meta: {
        title: "工作台"
      },
      // redirect: '/home',
      component: () => import('../views/layout.vue'),
      children: routes
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    return {top: 0}; // 总是滚动到顶部
  }
});

router.beforeEach(async (to, _from, next) => {

  // const store = useCommonStore();
  setPageTitle(to?.meta?.title);

  // const userInfo = store.loginUser;
  $nprogress.start();

  await next();

  $nprogress.done();
});

export default router
